<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * @see AbstractApplicationContext
 */
require_once 'AbstractApplicationContext.php';

/**
 * @see Menu
 */
require_once 'backend/Menu.php';

/**
 * @see MenuItem
 */
require_once 'backend/MenuItem.php';

/**
 *
 */
class BackendApplicationContext extends AbstractApplicationContext
{

    /**
     *
     * @var AdministratorEntity
     */
    private $currentAdministrator;

    /**
     * The backend menu.
     *
     * @var Menu
     */
    private $menu;

    /**
     *
     * @return AdministratorEntity
     */
    public function getCurrentAdministrator()
    {
        if ($this->currentAdministrator == null) {
            $this->currentAdministrator = $this->serviceFactory
                                               ->getAdministratorService()
                                               ->getAdministrator($this->getSessionUser()->getUserId());
        }
        return $this->currentAdministrator;
    }

    /**
     * Returns the current site.
     *
     * @return SiteEntity
     */
    public function getCurrentSite()
    {
        $currentSite = Session::get(Constants::SESSION_CURRENT_SITE);
        if ($currentSite == null) {
            $sites = $this->serviceFactory->getSiteService()->getSites();
            $currentSite = array_shift($sites);
            Session::set(Constants::SESSION_CURRENT_SITE, $currentSite);
        }
        return $currentSite;
    }

    /**
     * Returns the backend menu.
     *
     * @return Menu
     */
    public function getMenu()
    {
        if ($this->menu == null) {

            $this->menu = new Menu();
            
            $sessionUser = $this->getSessionUser();

            $currentSite = $this->getCurrentSite();
            $siteMenuItems = array();
            $sites = $this->serviceFactory->getSiteService()->getSites();
            
            foreach ($sites as $site) {
                $contentMenuItems = array();
                if ($sessionUser->hasPermission('page', Constants::ACCESS_LEVEL_VIEW, null, $site->id)) {
                    if ($site->id == $currentSite->id) {
                        $target = '/pages/';
                    } else {
                        $target = '/sites/select/' . $site->id . '/goto/pages/';
                    }
                    $contentMenuItems[] = new MenuItem('pages', $target, Translator::translate('Pages'));
                }
                if (count($contentMenuItems)) {
                    $siteMenuItem = new MenuItem('site' . $site->id, $contentMenuItems[0]->getTarget(), $site->name);
                    $siteMenuItem->setSubMenu(new Menu($contentMenuItems));
                    $siteMenuItems[] = $siteMenuItem;
                }
            }

            if (count($siteMenuItems)) {
                $contentMenu = new MenuItem('content', $siteMenuItems[0]->getTarget(), Translator::translate('Content'));
                $contentMenu->setSubMenu(new Menu($siteMenuItems));
                $this->menu->addMenuItem($contentMenu);
            }

            $systemMenuItems = array();
            if ($sessionUser->hasPermission('administrator', Constants::ACCESS_LEVEL_VIEW)) {
                $systemMenuItems[] = new MenuItem('administrators', '/administrators/', Translator::translate('Administrators'));
            }
            /*
            if ($sessionUser->hasPermission('plugin', Constants::ACCESS_LEVEL_VIEW)) {
                $systemMenuItems[] = new MenuItem('plugins', '/plugins/', Translator::translate('Plugins'));
            }
             */
            if ($sessionUser->hasPermission('setting', Constants::ACCESS_LEVEL_VIEW)) {
                $systemMenuItems[] = new MenuItem('settings', '/settings/', Translator::translate('Settings'));
            }
            if ($sessionUser->hasPermission('site', Constants::ACCESS_LEVEL_VIEW)) {
                $systemMenuItems[] = new MenuItem('sites', '/sites/', Translator::translate('Sites'));
            }
            if (count($systemMenuItems)) {
                $systemMenu = new MenuItem('system', $systemMenuItems[0]->getTarget(), Translator::translate('System'));
                $systemMenu->setSubMenu(new Menu($systemMenuItems));
                $this->menu->addMenuItem($systemMenu);
            }

//            $this->getEventHandler()
//                 ->dispatchEvent(Backend::EVENT_INIT_MENU, $this->menu);
        }
        return $this->menu;
    }

    /**
     * Returns the session user.
     *
     * @var SessionUser
     */
    public function getSessionUser()
    {
        $sessionUser = Session::get(Constants::SESSION_USER);
        if ($sessionUser == null) {
            $this->getRequest()->redirectTo('login');
        }
        return $sessionUser;
    }

    /**
     *
     * @return BackendApplicationContext
     * /
    protected function loadPlugins()
    {
        $plugins = $this->serviceFactory->getPluginService()->getPlugins();
        foreach ($plugins as $plugin) {
            if (!$plugin->isEnabled) {
                continue;
            }
            $pluginName = $plugin->name;
            require_once $this->getBasePath() . '/plugins/' . $pluginName . '/' . $pluginName . '.php';
            $plugin = new $pluginName($this, $this->serviceFactory);
            $this->plugins[$pluginName] = $plugin;
            $plugin->run();
        }
        return $this;
    }
     * 
     */

    /**
     * Sets the current site.
     *
     * @param SiteEntity $site
     * @return BackendApplicationContext
     */
    public function setCurrentSite(SiteEntity $site = null)
    {
        Session::set(Constants::SESSION_CURRENT_SITE, $site);
        return $this;
    }
}